package com.lancoo.edu.platform.operation.commons.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRawValue;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 旧版应用桌面对外接口返回结果
 *
 * @author lzh
 */
@Data
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultAppMgr<T> extends BaseResult {

    @JsonProperty(value = "ReturnCode")
    @JsonRawValue(value = true)
    private int code = 200;

    @JsonProperty(value = "ReturnCode_Msg")
    private String msg = "success";

    @JsonProperty(value = "ResultCode")
    @JsonRawValue(value = true)
    private int resultCode = 0;

    @JsonProperty(value = "ResultCode_Msg")
    private String resultCodeMsg = "success";

    @JsonProperty(value = "Data")
    private T data;

    /**
     * 构造无数据成功响应
     */
    public ResultAppMgr() {
    }

    /**
     * 构造无数据成功带说明响应
     *
     * @param msg 说明
     */
    public ResultAppMgr(String msg) {
        this.msg = msg;
        this.resultCodeMsg = msg;
    }

    /**
     * 构建无数据响应结果
     *
     * @param code 状态码
     * @param msg  说明
     */
    public ResultAppMgr(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.resultCode = code;
        this.resultCodeMsg = msg;
    }

    /**
     * 构建数据固定说明响应结果
     *
     * @param attribute domain实体
     */
    public ResultAppMgr(T attribute) {
        createQueryBean(attribute);
    }

    /**
     * 构建数据带说明响应结果
     *
     * @param msg       说明
     * @param attribute domain实体
     */
    public ResultAppMgr(String msg, T attribute) {
        this.msg = msg;
        this.resultCodeMsg = msg;
        createQueryBean(attribute);
    }

    /**
     * 构建数据响应结果
     *
     * @param code      状态码
     * @param msg       说明
     * @param attribute domain实体
     */
    public ResultAppMgr(int code, String msg, T attribute) {
        this.code = code;
        this.msg = msg;
        this.resultCode = code;
        this.resultCodeMsg = msg;
        createQueryBean(attribute);
    }

    public static <T> ResultAppMgr<T> success(T date) {
        ResultAppMgr<T> result = new ResultAppMgr<>(200, "success");
        result.setData(date);
        return result;
    }

    /**
     * 创建返回结果实体数据
     *
     * @param attribute 数据
     */
    private void createQueryBean(T attribute) {
        if (data == null) {
            data = attribute;
        }
    }
}
